<?php
/**
  * User.php
  *
  * This is not a freeware, use is subject to license terms
  * @author CLOUD DEV TEAM
  * 
  */
class User extends Model
{
	
	private static $object;
	
	private function User()
	{
		$this->tableName = 'user_info';
		parent::__construct();
	}
	
	public static function getInstance()
	{
		if ( !is_object( self::$object ) )
		{
			self::$object = new self();
		}
		return self::$object;
	}
	
	/**
	 * 验证用户名密码是否正确
	 * @param string $userName
	 * @param string $pass
	 */
	public function checkUser( $userName , $pass )
	{
		return $this->getOne( array( 'username' => $userName , 'password_md' => $pass ) ) ? true : false;
	}
	
	/**
	 * 
	 * 获取某个用户的信息
	 * @param int $userId
	 */
	public function getUserInfo( $userId )
	{
		return $this->getOne( array( 'uid' => $userId ) );
	}
	
	/**
	 * 验证邮箱是否有效
	 * @param string $email
	 */
	public static function validateEmail( $email )
	{
		if ( strlen($email) > 6 && preg_match('/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/' , $email ) )
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 验证用户名是否有效
	 * @param string $userName
	 */
	public static function validateUserName( $userName )
	{
		$guestexp = '\xA1\xA1|\xAC\xA3|^Guest|^\xD3\xCE\xBF\xCD|\xB9\x43\xAB\xC8';
		$len = strlen( $userName );
		if( $len > 15 || $len < 3 || preg_match( "/\s+|^c:\\con\\con|[%,\*\"\s\<\>\&]|$guestexp/is", $userName ) ) 
		{
			return false;
		} 
		return true;
	}
	
	/**
	 * 检测此用户名是否已存在
	 * @param string $userName
	 */
	public function checkUserNameExists( $userName )
	{
		if ( $this->getOne( array( 'username' => $userName ) ) )
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 登录操作
	 * @param string $userName
	 * @param string $pass
	 */
	public function login( $userName , $password )
	{
		$userInfo = $this->getOne( array( 'username' => $userName , 'password_md' => md5( $password ) ) );
		if ( $userInfo )
		{
			/*$data = array(
				'last_login_time' => SYSTIME,
				'last_login_ip' => IP,
			);
			$this->update( $data , array( 'user_id' => $userInfo['user_id'] ) );*/
			return $userInfo;
		}
		return false;
	}
	
	/**
	 * 注册新用户
	 * @param string $userName
	 * @param string $password
	 * @param string $email
	 */
	public function reg( $userName , $password , $email )
	{
		$data = array(
			'username' => $userName,
			'password_md' => md5( $password ),
			'email' => $email,
			'createdate' => date( "Y-m-d H:i:s" , SYSTIME ),
			'alterdate' => date( "Y-m-d H:i:s" , SYSTIME ),
		);
		if ( ( $userId = $this->insert( $data , true ) ) > 0 )
		{
			$userInfo = $this->getOne( array( 'uid' => $userId ) );
			return $userInfo;
		}
		return false;
	}
	
	/**
	 * 删除用户
	 * @param int $userId
	 * @param string $userName
	 */
	public function deleteUser( $userId = '' , $userName = '' )
	{
		$condition = '';
		if ( empty( $userId ) && empty( $userName ) )
		{
			return;
		}
		if ( $userId )
		{
			$condition = array( 'uid' => $userId );
		}
		elseif( $userName )
		{
			$condition = array( 'username' => $userName );
		}
		if ( $this->delete( $condition ) )
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 根据用户ID获取头像目录
	 * @param unknown_type $userId
	 */
	public static function computeAvatar( $userId )
	{
		$dir1 = $dir2 = '';
		if ( empty( $userId ) ) 
		{
			exit(0);
		}	
		$dir1 = ceil( $userId / 1000 );
		$dir2 = ceil( $userId % 1000 / 100 );
		$dir = 'Avatar/' . $dir1 . '/' . $dir2 . '/' . $userId . '/';
		if ( !file_exists( ROOT_DIR . $dir ) )
		{
			mkdir( ROOT_DIR . $dir , 0777 , true );
		}
		return $dir;
	}
	
	/**
	 * 获取头像的链接
	 * @param int $userId
	 */
	public static function getAvatarUrl( $userId )
	{
		return ATTACHMENTURL . '/' . self::computeAvatar( $userId );
	}
}

